import React, {Component} from 'react';
import {connect} from 'dva';
import moment from 'moment'
import md5 from 'md5'

import {Row,Col} from 'antd'

import style from './home.less'

import {Tabtitle} from '../../../components'
import ToolBar from '../components/ToolBar'

const cptList=[
	{
		id:1,
		name:'股票型',
		value:'thfx/tgjxpg_stock.cpt',
	},
	{
		id:2,
		name:'债券型',
		value:'thfx/tgjxpg_bond.cpt',
	},
	{
		id:3,
		name:'期货型',
		value:'thfx/tgjxpg_futures.cpt',
	},
	{
		id:4,
		name:'混合型',
		value:'thfx/tgjxpg_mixed.cpt',
	},
]

const Standard = ({dispatch,standard,location:{pathname,query}}) =>{
	const tag = `standard`;
	const urlArr = [];
	const {
		//基准ID
		benchId,
		//报表类型
		fundStyle,
		showReport,
		pdtName,
		...others,
	}=standard

	const {
		portId,
		portList,
		beginDate,
		endDate,
		timeSlot,
		quarter,
		sceneId,
		sceneList,
	}=others

	const setState=(payload) => {
		console.log(payload);
		dispatch({
			type:`${tag}/setState`,
			payload
		})
	}

	// 查询情景
	const searchSence = (payload) => {
		if(payload.portId&&payload.portId!=''){
			dispatch({
				type:`${tag}/queryReportParams`,
				payload
			})
		}
	}

	// 查询报表方法
	const searchReport = (payload) => {
		const timestampS=Date.parse(new Date())/1000
		dispatch({
			type:`${tag}/changeRouter`,
			payload:{
				pathname:`/investmentSummary/investReport/${timestampS}`,
				query:{
					...payload,
					// at:timestamp
				}
			}
		})
	}

	

	let iframeUrl='',exportUrl=''

	if(	portId&&portId!=''&&portList&&portList.length&&((sceneId!='0'&&sceneList&&sceneList.length)||(sceneId=='0'&&sceneList&&sceneList.length==0))&&fundStyle){
		const cpt=cptList.find(item=>item.id==fundStyle).value
		const timestamp=parseInt(moment().format('x')/(1000*60*60*8))//8小时
		const sign=md5(`${cpt}thfxthfx${timestamp}6f8cda41e3ce45318c4bad855f2abb37`)
		const host=window.location.href.split('#')[0]
		let hostname=''

		if(host.indexOf('eip.')>-1){
			// 华泰OA
			hostname=`http://eip.htsc.com.cn`
		}else if(host.indexOf('168.9.')>-1){
			// 生产环境
			hostname=`http://168.6.30.100:56780`
		}else{
			// 开发环境
			hostname='http://168.61.13.30:56780'
		}

		const url=`${hostname}/finereport/ReportServer?reportlet=${cpt}&op=write&platform=thfx&userLogin=thfx&sign=${sign}`
		iframeUrl=`${url}&PORT_ID=${portId}&BEGIN_DATE=${moment(beginDate,'YYYYMMDD').format('YYYY-MM-DD')}&END_DATE=${moment(endDate,'YYYYMMDD').format('YYYY-MM-DD')}&SIMULATION_ID=${sceneId}&BENCH_ID=${benchId}`
		exportUrl=`${iframeUrl}&format=pdf&__filename__=${portList.find(item=>item.key===standard.portId).label}_${beginDate}_${endDate}`
		// urlArr.push(url)
		// console.log('cccccc',)
	}



		return (
			<div className={`equity ${style.standard}`}>
				<Row type="flex" justify="start" align='middle'>
					{/*<Col span={3}> */}
						<Tabtitle value={'标准分析报告'}/>
					{/*</Col>*/}
					{
						pdtName?<Col span={5}>
							<div className={style.pdtNamecss}>&mdash;&mdash;{pdtName}</div>
						</Col>:null
					}
					
				</Row>
				<div className="content-inner">
					{
						portList&&portList.length?<ToolBar {...standard} setState={setState} searchSence={searchSence} searchReport={searchReport} url={exportUrl} />:null
					}
					{
						showReport?(
							<div className={style.mainFrame} style={{height:`${document.documentElement.clientHeight-275}px`}}>
								<iframe
									src={iframeUrl}
									width="100%"
									height="100%"
									frameBorder="0"
									marginWidth="0"
									marginHeight="0"
									allowTransparency="yes"
									></iframe>
							</div>
						):null
					}

				</div>
			</div>
		)
}

Standard.propTypes = {
};

export default connect(({standard}) => ({standard}))(Standard);
